﻿<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<dynamic>" %>

<asp:Content ID="Content1" ContentPlaceHolderID="PageTitle" runat="server">
    <title>易得桌面</title>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="PageStyles" runat="server">
    <link href="../../Content/freecontent.css" rel="stylesheet" type="text/css" />
    <style type="text/css">
        .fieldslist
        {
            word-break: break-all;
            word-wrap: break-word;
        }
        .expText
        {
            width: 280px;
        }
    </style>
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="PageScripts" runat="server">
    <script type="text/javascript">
    //************* URL常量
    var URL_APPLY_MODITY_ATTENDANCE_EVALUATION = "/Attendance/ApplyModifyAttendanceEvaluation";


    //************* 全局变量
    var g_Var = {
        "applyUserId": null,
        "time": null,
        "applicationWork": null
    };


    //************* 常量定义
    var StaticResource = {
        "ApplicationState": {
            "Applying": "您已申请修改考勤评估，等待处理中。",
            
        },
        "WeekendInfo": {
            "false": "非周末",
            "true": "周末"
        },
        "IsNeedToWork": {
            "false": "休息",
            "true": "正常上班"
        },
        "timeTarget": {
            "minute": "分钟",
            "hour": "小时"
        },

        "YesOrNoTarget": {
            "false": "否",
            "true": "是",
            "False": "否",
            "True": "是"
        },

        "ItemName": {
            "LateMiniutes": "迟到",
            "EarlyMiniutes": "早退",
            "IsAbsense": "旷工",
            "IsExtraWork": "加班",
            "ManHours": "获得工时"
        }

    };



    //************* Accessor访问器
    var EzAccessor = {};
    function CreateAccessor() {
            EzAccessor = CreateRealEzAccessor();
    }


    function CreateRealEzAccessor() {
        //实现服务器交互方法
        var realEzAccessor = {
            "getDayInfo": function () {
                                    return <%= ViewData["DayInfo"] %>;
            },
            "getIsDisplayAttendanceInfo": function () {
                                    return <%= ViewData["IsDisplayAttendanceInfo"] %>;
            },
            "getIsDisplayApplyEvaluation": function () {

                                    return <%= ViewData["IsDisplayApplyEvaluation"]%>;
            }, 
            "getIsDisplayEvaluationInfo": function () {

                                    return <%= ViewData["IsDisplayEvaluationInfo"]%>;
            },
            "getIsApplyingModify": function () {

                                    return <%= ViewData["IsApplyingModify"]%>;
            },
            "getEvaluationModifyApplicaitons": function () {
                                    return <%= ViewData["ModifyAttendanceApplication"] %>;
            },
            "getTargetUser": function () {
                                    return <%= ViewData["TargetUser"]%>

            },

            "getDayAttendanceDetail": function () {
                                    return <%= ViewData["DayAttendanceDetail"] %>;
            },

            "geEvaluationModifyHistory": function () {
                                    return <%= ViewData["EvaluationModifyHistory"] %>;
            },

            "doApplyModify": function (applyUserId, date, applyText) {
                $(document).ajaxBusy('begin');
                $.post(
                    URL_APPLY_MODITY_ATTENDANCE_EVALUATION,
                    {
                        'applyUserId': applyUserId,
                        'date': date,
                        'applyText': applyText
                    },
                    function (e) {
                        $(document).ajaxBusy('end');
                        $.displayStatusInfo(e);

                        window.location.href = '/Attendance/PersonalAttendanceInfoPerDay?time=' + date;
                    }
                );
            }
        };

        return realEzAccessor;
    }


    function pageReload() {
        window.location.reload();

    }


    //上下班打卡考勤记录
    function bindCheckAttendanceDetail(attendanceDetail) {
        var attendanceInfoList = attendanceDetail.AttendanceInfoList;
        for (var key in attendanceInfoList) {
            var actionTime = Date.fromEpoch(attendanceInfoList[key].ActionTime).toFullTimeFormat();
            var isAbnormal = attendanceInfoList[key].IsAbnormalAttendance ? '<span class="important">*</span>' : "";
            var explanation = attendanceInfoList[key].Explanation;
            var explanationStr = (explanation == null)? "" : explanation; 
            var attendanceDom = '<p>{isAbnormal}{actionTime}</p><p class="expText">{Explanation}</p>';
            attendanceDom = attendanceDom.replace("{actionTime}", actionTime)
                                         .replace("{isAbnormal}", isAbnormal)
                                         .replace("{Explanation}", explanationStr);
            //上班
            if (attendanceInfoList[key].AttendanceType == "onDuty") {
                $('#actionTimeOnDuty').append(attendanceDom);
            }
            //下班
            else {
                $('#actionTimeOffDuty').append(attendanceDom);
            }

        }

    }


    //假期信息
    function bindLeaveInfoDetail(attendanceDetail) {
        if (attendanceDetail.LeaveInfo) {
            var leaveInfo = attendanceDetail.LeaveInfo.Leaves;
            for (var key in leaveInfo) {
                var leaveName = leaveInfo[key].LeaveName;
                var startTime = Date.fromEpoch(leaveInfo[key].StartTime);
                var endTime = Date.fromEpoch(leaveInfo[key].EndTime);
                var startTimeFormat = startTime.toFullTimeFormat();
                var endTimeFormat = endTime.toFullTimeFormat();
                var leaveTime = startTimeFormat + " 至 " + endTimeFormat;
                var leaveInfoDom = '<p>{leaveName} {leaveTime}</p>';


                leaveInfoDom = leaveInfoDom.replace("{leaveName}", leaveName)
                                           .replace("{leaveTime}", leaveTime);

                $('#cmbLeaveinfo').append(leaveInfoDom);

            }
        }
    }



    //考勤评估信息
    function bindAttendanceEvaluationDetail(attendanceDetail) {
        var attendanceEvaluation = attendanceDetail.AttendanceEvaluation;
        if (attendanceEvaluation) {
            var lateMiniute = attendanceEvaluation.LateMiniutes;
            var earlyMiniutes = attendanceEvaluation.EarlyMiniutes;
            var isAbsense = StaticResource.YesOrNoTarget[attendanceEvaluation.IsAbsense];
            var isExtraWork = StaticResource.YesOrNoTarget[attendanceEvaluation.IsExtraWork];
            var manHours = (attendanceEvaluation.Manhours) / 10.00;

            $('#cmbBeLate').html(lateMiniute + StaticResource.timeTarget.minute);
            $('#cmbLeaveEarly').html(earlyMiniutes + StaticResource.timeTarget.minute);
            $('#cmbAbsent').html(isAbsense);
            $('#cmbExtraWork').html(isExtraWork);
            $('#cmbManHours').html(manHours + StaticResource.timeTarget.hour);
        }
    }



    function bindAttendanceDetail(attendanceDetail, IsDisplayEvaluationInfo) {
        bindCheckAttendanceDetail(attendanceDetail);
        bindLeaveInfoDetail(attendanceDetail);

        if (IsDisplayEvaluationInfo) {
            bindAttendanceEvaluationDetail(attendanceDetail);
        }else{
            $('#attendaceEvl').hide();
        }
    }




   
    //修改记录
    function showEvaluationModifyHistory(evaluationModifyHistory) {
        var evaluationChange = "";
        if(evaluationModifyHistory){
            for (var key in evaluationModifyHistory) {
                var modifyOn = Date.fromEpoch(evaluationModifyHistory[key].ModifyOn);
                var modifyOnFormat = modifyOn.toShortDateTime();
                var operatorName = evaluationModifyHistory[key].Operator.Name;
                //修改人和修改时间
                var modifyTittle = '<p style="margin-top:10px;">{operatorName} 在 {modifyOnFormat}</p>';
                modifyTittle = modifyTittle.replace("{operatorName}", operatorName)
                                           .replace("{modifyOnFormat}", modifyOnFormat);

                //修改内容
                var modifyList = '';
                var modifyInfo = '';
                var attendanceEvaluationChangeList = evaluationModifyHistory[key].AttendanceEvaluationChangeList;
                
                for (var target in attendanceEvaluationChangeList) {
                    var itemName = StaticResource.ItemName[attendanceEvaluationChangeList[target].ItemName];
                    var oldValue = attendanceEvaluationChangeList[target].OldValue;
                    var newValue = attendanceEvaluationChangeList[target].NewValue;
                    
                    oldValue = editEvaluationItemValue(itemName, oldValue);
                    newValue = editEvaluationItemValue(itemName, newValue);

                    modifyInfo = modifyInfo + '<p>将 {itemName} {oldValue} 修改为 {newValue}</p>';
                    modifyInfo = modifyInfo.replace("{itemName}", itemName)
                                          .replace("{oldValue}", oldValue)
                                          .replace("{newValue}", newValue);

                }
                if (attendanceEvaluationChangeList == null) {
                    modifyInfo = '拒绝了考勤评估修改申请。';
                }
                evaluationChange = evaluationChange + modifyTittle + modifyInfo;
            }
            
            $("#cmbEvaluationModifyHistory").append(evaluationChange);
            EzTipsManager.showTipBox('tipModifyInfo');
        }
    }




    function showApplicationList(application) {
        if(application) {
            var applyUserName = application.ApplyUser.Name;
            var applyOn = Date.fromEpoch(application.ApplyOn);
            var applyOnFormat = applyOn.toShortDateTime();
            var applyText = application.ApplyText;
            var applyState = application.State;

            if(applyState == "Applying"){
                $('#txtWarn').html(StaticResource.ApplicationState.Applying);
                $('#txtWarn').css({
                                    'font-size': '16px',
                                    'font-weight':'bold',
                                    'color': 'red'
                                 });
            }

            var applyInfoDom = '<p><span>{applyUserName} 于 {applyOn} 申请</span></p><p> 修改原因： <span >{applyText}</span></p>';
            applyInfoDom = applyInfoDom.replace("{applyUserName}", applyUserName)
                                        .replace("{applyOn}", applyOnFormat)
                                        .replace("{applyText}", applyText);
                                        
            $('#cmbApplyInfo').append(applyInfoDom);
            EzTipsManager.showTipBox('tipApplyInfo');
            
        }

    }
    //显示当日信息
    function showDayInfo(dayInfo) {
        var isWeekEnd = StaticResource.WeekendInfo[dayInfo.IsWeekEnd];
        var needToWork = StaticResource.IsNeedToWork[dayInfo.IsNeedToWork];
        var extraShiftInfo = "";
        var holidayInfo = "";
        if (dayInfo.ExtraShiftInfo && dayInfo.HolidayInfo) {
            extraShiftInfo = dayInfo.ExtraShiftInfo.Name
            holidayInfo = dayInfo.HolidayInfo.Name;
        }

        var dayInfoDom = '<p>当日信息 : {weekendInfo}  {holidayInfo}  {extraShiftInfo} {needToWork}</p>';
        dayInfoDom = dayInfoDom.replace("{weekendInfo}", isWeekEnd)
                               .replace("{holidayInfo}", holidayInfo)
                               .replace("{extraShiftInfo}", extraShiftInfo)
                               .replace("{needToWork}", needToWork);

        $('#lblDayInfo').append(dayInfoDom);
    }




    //Tittle
    function showApplicationTittle(currentUser, dayInfo) {
        var applyUserName = currentUser.Name;
        var nowDay = Date.fromEpoch(dayInfo.Date).toFullChineseDateFormat();
        var tittleStr = applyUserName + " " + nowDay + " " + "考勤详细情况";

        $('#lblTittle').html(tittleStr);
    }



    function editEvaluationItemValue(itemName, value) {
        var editedValue = value + StaticResource.timeTarget.minute;
        switch (itemName) {
            case StaticResource.ItemName.ManHours: editedValue = (value / 10) + StaticResource.timeTarget.hour;
                break;
            case StaticResource.ItemName.IsExtraWork: editedValue = StaticResource.YesOrNoTarget[value];
                break;
            case StaticResource.ItemName.IsAbsense: editedValue = StaticResource.YesOrNoTarget[value];
                break;

        }

        return editedValue;

    }



   

    function showApplicationDetail(dayInfo, targetUser) {

        showDayInfo(dayInfo);
        showApplicationTittle(targetUser, dayInfo);
    }



    function initPage() {
        EzTipsManager.addTipBox('#tip_Remind', 'tipRemind');
        EzTipsManager.addTipBox('#tip_ApplyInfo', 'tipApplyInfo');
        EzTipsManager.addTipBox('#tip_ModifyInfo', 'tipModifyInfo');
        
        EzTipsManager.showTipBox('tipRemind');
        
        //获取数据
        CreateAccessor();

        var dayInfo = EzAccessor.getDayInfo();
        var isDisplayAttendanceInfo = EzAccessor.getIsDisplayAttendanceInfo();
        var targetUser = EzAccessor.getTargetUser();

        g_Var["time"] = dayInfo.Date;
        g_Var["applyUserId"] = targetUser.Id;

        //显示当日信息，申请信息，评估历史等
        showApplicationDetail(dayInfo, targetUser);

        if (isDisplayAttendanceInfo) {
            var application = EzAccessor.getEvaluationModifyApplicaitons();
            var isDisplayApplyEvaluation = EzAccessor.getIsDisplayApplyEvaluation();
            var attendanceDetail = EzAccessor.getDayAttendanceDetail();
            var evaluationModifyHistory = EzAccessor.geEvaluationModifyHistory();
            var IsDisplayEvaluationInfo = EzAccessor.getIsDisplayEvaluationInfo();
            var IsApplyingModify = EzAccessor.getIsApplyingModify();
            //已存在申请
            showApplicationList(application);
            //修改历史
            showEvaluationModifyHistory(evaluationModifyHistory);
            //考勤信息
            bindAttendanceDetail(attendanceDetail, IsDisplayEvaluationInfo);
            //是否可以申请修改
            if (!isDisplayApplyEvaluation) {
                $('#applyModify').hide();
            }else{
                $('#applyModify').show();
            }

            //未来时间不可查看
            if(!IsDisplayEvaluationInfo){
                $('#nextDay').hide();    
            }
            //是否正在申请
            if(IsApplyingModify){
                showApplyContent();
            }
        }
        else {
            $('#tblNoFuture').show();
            $('#nextDay').hide();
            $('#preDay').hide();
            $('#tblAttendanceInfo').hide();
        }

    }

    function  showApplyContent() {
        var divDom = $("<div/>");
        var htmlText = '<p>请填写申请修改考勤评估的原因:</p>' +
                        '<textarea cols="43" rows="4" id="txtExplain"></textarea>' +
                        '<div>' +
                                '<input id="btnSubmit" style="width: 80px; height: 30px;" type="button" value="确定" />' +
                        '</div>';

        $(divDom).html(htmlText);

        divDom.contentsManager({ "width": "390px",
            "height": "160px"
        });
        //确定申请
        $('#btnSubmit').click(function () {
            var applyUserId = g_Var["applyUserId"];
            var date = g_Var["time"];
            var applyText = $('#txtExplain').val().htmlEncode();

            EzAccessor.doApplyModify(applyUserId, date, applyText);

            $('#txtExplain').clearFields();

            $('#fade, .popup_block').fadeOut();

            $('#shortcuts').hide();
        
        });

    }

    function initPageEvent() {

        //申请修改
        $('#applyModify').click(function () {
            showApplyContent();
        });


        $('#preDay').click(function () {
            var date = Date.fromEpoch(g_Var["time"]);

            var perDay = new Date(
                date.getFullYear(),
                date.getMonth(),
                date.getDate() - 1,
                0,
                0,
                0
            ).toEpoch();

            window.location.href = "PersonalAttendanceInfoPerDay?time="
                           + perDay;

        });


        $('#nextDay').click(function () {
            var date = Date.fromEpoch(g_Var["time"]);

            var nextDay = new Date(
                date.getFullYear(),
                date.getMonth(),
                date.getDate() + 1,
                0,
                0,
                0
            ).toEpoch();

            window.location.href = "PersonalAttendanceInfoPerDay?time=" + nextDay;

        });

        $('.btn-shortcut').click(function (){
            window.location.href = '/Attendance/PersonalAttendanceInfoList?time=' + g_Var["time"];
        });


    }


    $(function () {
        initPage();
        initPageEvent();
    });
    </script>
</asp:Content>
<asp:Content ID="Content4" ContentPlaceHolderID="PageHead" runat="server">
    <%
        Html.RenderPartial("_sayHello"); %>
    <div id="status-bar"><% Html.RenderPartial("_navigateTo"); %></div>
</asp:Content>
<asp:Content ID="Content5" ContentPlaceHolderID="PageContent" runat="server">
    <div id="page-head">
        <img src="../../Content/icons/attendance_perday.png" style="width: 48px; height: 48px;"
            alt="personalPerday" />
        <span id="lblTittle"></span>
        <div style="margin-top: 5px; min-height: 20px;">
            <div style="float: left;">
                <a id="preDay">前一天</a>
            </div>
            <div style="float: right;">
                <a id="nextDay">后一天</a>
            </div>
        </div>
    </div>
    <div id="page-body">
        <div id="tblNoFuture" style="display: none;">
            <%Html.RenderPartial("_noFutureDisplayInfo"); %>
        </div>
        <div id="tblAttendanceInfo">
            <div>
                <ul class="fieldslist">
                    <li>
                        <div class="l">
                            上班：
                        </div>
                        <div id="actionTimeOnDuty" class="r">
                        </div>
                    </li>
                    <li>
                        <div class="l">
                            下班：
                        </div>
                        <div id="actionTimeOffDuty" class="r">
                        </div>
                    </li>
                </ul>
                <ul class="fieldslist">
                    <li>
                        <div class="l">
                        </div>
                        <div id="cmbLeaveinfo" class="r">
                        </div>
                    </li>
                </ul>
            </div>
            <div id="attendaceEvl">
                <p class="entry-explanation">
                    考勤评估记录</p>
                &nbsp;<span id="txtWarn">如果您的考勤评估信息与实际情况存在出入,或者因为其他原因,可以申请修改相应信息。</span> &nbsp;<a
                    id="applyModify" class="highlight" style="display: none;">申请修改考勤评估</a>
                <div style="border: 1px solid #090; margin-top: 10px;">
                    <ul class="fieldslist">
                        <li>
                            <div class="l">
                                迟到：
                            </div>
                            <div class="r">
                                <p id="cmbBeLate">
                                </p>
                            </div>
                        </li>
                        <li>
                            <div class="l">
                                早退：
                            </div>
                            <div class="r">
                                <p id="cmbLeaveEarly">
                                </p>
                            </div>
                        </li>
                        <li>
                            <div class="l">
                                旷工：</div>
                            <div class="r">
                                <p id="cmbAbsent">
                                </p>
                            </div>
                        </li>
                        <li>
                            <div class="l">
                                加班：</div>
                            <div class="r">
                                <p id="cmbExtraWork">
                                </p>
                            </div>
                        </li>
                        <li>
                            <div class="l">
                                工时：
                            </div>
                            <div class="r">
                                <p id="cmbManHours">
                                </p>
                            </div>
                        </li>
                    </ul>
                </div>
            </div>
        </div>
    </div>
    <div class="page-submit" style="display: none;">
        <div style="margin: 20px 20px 30px 30px;">
            <div class="formBox">
            </div>
        </div>
    </div>
</asp:Content>
<asp:Content ID="Content6" ContentPlaceHolderID="PageShortcut" runat="server">
    <div id="shortcuts" class="box">
        <a class="btn-shortcut">查看考勤记录</a>
    </div>
</asp:Content>
<asp:Content ID="Content7" ContentPlaceHolderID="PageFoot" runat="server">
    <div id="tip_Remind" style="display: none;">
        <h1>
            小贴士:</h1>
        <p class="indent">
        </p>
    </div>
    <div id="tip_ApplyInfo" style="display: none;" class="tip-succ">
        <h1>
            考勤评估修改申请:</h1>
        <div id="cmbApplyInfo" class="indent">
        </div>
    </div>
    <div id="tip_ModifyInfo" style="display: none;" class="tip-memo">
        <h1>
            考勤评估修改历史:</h1>
        <div id="cmbEvaluationModifyHistory" class="indent">
        </div>
    </div>
    <div id="popContent" style="display: none;">
    </div>
</asp:Content>
